package ext.tianma.part.statute;

import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.List;

import org.apache.log4j.Logger;

import ext.com.iba.IBAUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.TianMaConstants;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.WTObject;
import wt.log4j.LogR;
import wt.org.DirectoryContextProvider;
import wt.org.GenericDirectoryContextProvider;
import wt.org.OrganizationServicesHelper;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.project.Role;
import wt.team.Team;
import wt.util.WTException;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WfAssignedActivity;
/**
 * 用来原材料验证料号生效地是否包含C01/02、04、08,环保等级是否为"RoHS" "客户特殊要求", 并指定法规审核者
 * 
 * @author laichao_sun
 *
 */

public class StatuteAssessor {
	private static final Logger log = LogR.getLogger(StatuteAssessor.class.getName());
	// 角色为法规审核
	private static String internal_value = "lawAssessor";
	// 固定参与者为李在纯
	private static String name = "A050408";

	/**
	 * 验证料号生效地是否包含C01/02、04、08,环保等级是否为"RoHS" partNumberEffectiveSite
	 * partEnvProtectionCategory
	 * 
	 * @param object
	 * @return
	 * @throws RemoteException
	 * @throws WTException
	 */
	public static boolean getPartNESiteAndEPC(WTObject object) throws RemoteException, WTException {
		boolean flag = false;
		String partNumberEffectiveSite = "";
		String partEnvProtectionCategory = "";
		WTPart part = null;
		if (object instanceof WTPart) {
			part = (WTPart) object;
			partNumberEffectiveSite = IbaAttributeUtil.getAttributeValue(part, "partNumberEffectiveSite");
			partEnvProtectionCategory = IbaAttributeUtil.getAttributeValue(part, "partEnvProtectionCategory");
			log.info("partNumberEffectiveSite=" + partNumberEffectiveSite);
			log.info("partEnvProtectionCategory=" + partEnvProtectionCategory);
			boolean flag2 = checkEffectiveSite(part);
			if(flag2) {
			if (partEnvProtectionCategory.equals("RoHS")||partEnvProtectionCategory.equals("客户特殊要求")) {
					return true;
			}
			}
		}
		return flag;
	}


	/*
	 * 为法规审核者指定固定的人: 1.获取工作流里的当前团队 2.获取团队中的承担的角色 3.在获取名字为name的人user 4.将参与者添加到团队中
	 */
	public static void addLawAssessorToRole(ObjectReference self) throws Exception {
		// 获取当前的工作流进程
		WfProcess wfProcess = getWfProcess(self);
		// 获取流程中的团队
		Team team = ext.workflow.WorkFlowUtil.getTeam(wfProcess);
		// 获取指定的用户
		WTUser user = getUserByName1(name);
		log.info("法规审核人为:" + user.getName());
		String rolename = internal_value;
		Role role = Role.toRole(rolename);
		team.addPrincipal(role, user);
		PersistenceHelper.manager.refresh(team);
	}

	// 检验料号生效地是否有产地C01/02、04、08
	private static boolean checkEffectiveSite(WTPart part) {
		List<String> effectiveSite = IbaAttributeUtil.getAttributeValues(part, "partNumberEffectiveSite");
		boolean temp = false;
		for (String site : effectiveSite) {
			if (site.contains(TianMaConstants.EffectiveSite.C01.getSite())||site.contains(TianMaConstants.EffectiveSite.C02.getSite())||site.contains(TianMaConstants.EffectiveSite.C04.getSite())||site.contains(TianMaConstants.EffectiveSite.C08.getSite())) {
				temp = true;
				break;
			}
		}
		return temp;
	}

	/**
	 * 根据name获得用户
	 * 
	 * @param name
	 * @return
	 */
	private static WTUser getUserByName1(String name) {
		WTUser user = null;
		try {
			String[] defaultService = OrganizationServicesHelper.manager.getDirectoryServiceNames();
			DirectoryContextProvider dcp = OrganizationServicesHelper.manager
					.newDirectoryContextProvider(defaultService, new String[] { "subtree" });
			Enumeration eee = OrganizationServicesHelper.manager.findLikeUsers("name", name,
					new GenericDirectoryContextProvider(defaultService));
			if (eee.hasMoreElements())
				user = (WTUser) eee.nextElement();
		} catch (WTException wte) {
			wte.printStackTrace();
		}
		return user;
	}

	public static WfProcess getWfProcess(ObjectReference self) throws WTException {
		if (self == null) {
			return null;
		}
		Persistable persistable = self.getObject();
		if (persistable instanceof WfAssignedActivity) {
			return ((WfAssignedActivity) persistable).getParentProcess();
		} else if (persistable instanceof WfProcess) {
			return (WfProcess) persistable;
		}
		return null;
	}

}
